#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年3月6日

@author: chenyongbing
'''
import sys,os,datetime
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]




"""
Demonstrates how to use the blocking scheduler to schedule a job that executes on 3 second intervals.
"""


from datetime import datetime
import os
from funcs import *
import logging
logger = logging.basicConfig()
from mysqlalchemy import  SQLAlchemyJobStore
#from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.base import BaseScheduler
from apscheduler.schedulers.blocking import BlockingScheduler
myJobStore = SQLAlchemyJobStore(url='mysql://root:zhimatech@localhost/scheduler')

class SchedulerJobs():
    def get_all_jobs(self):
        jobs = myJobStore.get_all_jobs(status=None)
        return self.analyze_jobs(jobs)
        
    
    def get_active_jobs(self):
        jobs = myJobStore.get_all_jobs(status=1)
        return self.analyze_jobs(jobs)
    def get_inactive_jobs(self):
        jobs = myJobStore.get_all_jobs(status=0)
        return self.analyze_jobs(jobs)

    def analyze_jobs(self,jobs):
        datas = []
        for job in jobs:
            name  = job.name
            id = job.id
            trigger_interval = job.trigger.interval
            next_run_time = job.next_run_time
            datas.append({'id':id,'name':name,'trigger_interval':trigger_interval,'next_run_time':next_run_time})
        return datas
mySchedulerJobs  = SchedulerJobs()
if __name__ == '__main__':
    print mySchedulerJobs.get_all_jobs()